package com.jcfk.eam.controller.mould;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.enums.DeleteFlag;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.*;
import com.jcfk.eam.controller.parent.EamAssetInfoController;
import com.jcfk.eam.domain.dto.base.EamAssetTypeDTO;
import com.jcfk.eam.domain.dto.main.EamAssetInfoDTO;
import com.jcfk.eam.domain.dto.mould.EamMouldTaskInfoDetailDTO;
import com.jcfk.eam.domain.query.main.EamAssetInfoQuery;
import com.jcfk.eam.domain.query.mould.EamMouldTaskInfoDetailQuery;
import com.jcfk.eam.enums.AssetKind;
import com.jcfk.eam.service.base.EamAssetTypeService;
import com.jcfk.eam.service.base.EamSopStandardService;
import com.jcfk.eam.service.equ.EquAssetInfoService;
import com.jcfk.eam.service.main.EamAssetInfoService;
import com.jcfk.eam.service.mould.EamMouldTaskInfoDetailService;
import com.jcfk.eam.service.mould.MouldAssetInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 模具台账 服务接口
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-07-28
 */
@Api(tags = {"模具台账"})
@RestController
@RequestMapping("mouldAssetInfo")
public class MouldAssetInfoController {
    @Autowired
    private EamAssetInfoController assetInfoController;

    @Autowired
    private EquAssetInfoService equAssetInfoService;

    @Autowired
    private EamAssetInfoService eamAssetInfoService;

    @Autowired
    private EamAssetTypeService eamAssetTypeService;

    @Autowired
    private EamSopStandardService eamSopStandardService;

    @Autowired
    private MouldAssetInfoService mouldAssetInfoService;
    @Autowired
    private EamMouldTaskInfoDetailService eamMouldTaskInfoDetailService;

    /**
     * 分页列表
     */
    @ApiOperation("分页查询模具台账")
    @PostMapping("/queryByPage")
    public RetPage<EamAssetInfoDTO> queryByPage(@RequestBody ReqPageParam<EamAssetInfoQuery> requestParam) {
        requestParam.getData().setAssetKind(AssetKind.MOULD.getValue());
        // 设置默认排序
        if (requestParam.getSort().getOrderItem() == null) {
            ReqSortParam<EamAssetInfoQuery> sortParam = new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }

        // 设置设备类型查询条件
        if (!StrUtil.isEmpty(requestParam.getData().getAssetTypeId())) {
            EamAssetTypeDTO eamAssetTypeDTO = eamAssetTypeService.get(requestParam.getData().getAssetTypeId());
            if (eamAssetTypeDTO != null) {
                requestParam.getData().setAssetTypeId(eamAssetTypeDTO.getAllId());
            }
        }

        DataPage<EamAssetInfoDTO> page = new DataPage<EamAssetInfoDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamAssetInfoDTO.class);
        page = eamAssetInfoService.queryMouldByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamAssetInfoQuery", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("查询模具列表")
    @PostMapping("/getList")
    public RetResult<List<EamAssetInfoDTO>> getList(@RequestBody EamAssetInfoQuery requestParam) {
        if (requestParam.getDeleted() == null) {
            requestParam.setDeleted(DeleteFlag.NOTDELETED.getValue());
        }
        if (requestParam.getEnabled() == null) {
            requestParam.setEnabled(AvailableStatus.ENABLE.getValue());
        }
        requestParam.setAssetKind(AssetKind.MOULD.getValue());
        return RetResult.ok(eamAssetInfoService.getList(requestParam));
    }

    /**
     * 模具台账
     * 根据id 查询详情
     */
    @ApiOperation("查询模具台账")
    @GetMapping("/get/{id}")
    public RetResult<EamAssetInfoDTO> get(@PathVariable("id") String id) {
        RetResult<EamAssetInfoDTO> itemResult = assetInfoController.get(id);
        return itemResult;
    }

    /**
     * 模具台账
     * 根据模具code 查询详情
     */
    @ApiOperation("根据模具code查询模具台账")
    @GetMapping("/getInfo/{code}")
    public RetResult<EamAssetInfoDTO> getInfobyCode(@PathVariable("code") String code) {
        EamAssetInfoDTO eamAssetInfoDTO = eamAssetInfoService.getInfobyCode(code);
        return RetResult.ok(eamAssetInfoDTO);
    }

    @GetMapping("/selectByProductId")
    public RetResult<List<EamAssetInfoDTO>> selectByProductId(@RequestParam("productId") String productId) {
        return RetResult.ok(eamAssetInfoService.selectByProductId(productId));
    }

    /**
     * 检具台账
     * 根据台账状态提示能否出库
     */
    @ApiOperation("根据设备code查询设备台账出库")
    @GetMapping("/checkCode/{code}")
    public RetResult<EamAssetInfoDTO> checkCode(@PathVariable("code") String code) {
        EamAssetInfoDTO eamAssetInfoDTO = eamAssetInfoService.getInfobyMould(code);
        if (eamAssetInfoDTO == null) {
            return RetResult.fail("模具编号不存在！");
        }
        if (null == eamAssetInfoDTO.getStorageStatus() || 1 != eamAssetInfoDTO.getStorageStatus()) {
            return RetResult.fail("模具状态不是在库！不能出库");
        }
        if (eamAssetInfoDTO.getAssetStatus() == null || eamAssetInfoDTO.getAssetStatus() != 3) {
            return RetResult.fail("模具状态不是正常状态,无法出库");
        }
        EamMouldTaskInfoDetailQuery query = new EamMouldTaskInfoDetailQuery();
        query.setMouldCode(code);
        query.setOutboundStatus(0);
        if (0 < eamMouldTaskInfoDetailService.getList(query).size()) {
            return RetResult.fail("该模具已扫码登记！");
        }
        return RetResult.ok(eamAssetInfoDTO);
    }

    /**
     * 检具台账
     * 根据台账状态提示能否出库
     */
    @ApiOperation("根据设备code查询设备台账入库")
    @GetMapping("/checkCodeIn/{code}")
    public RetResult<EamAssetInfoDTO> checkCodeIn(@PathVariable("code") String code) {
        EamAssetInfoDTO eamAssetInfoDTO = eamAssetInfoService.getInfobyMould(code);
        if (eamAssetInfoDTO == null) {
            return RetResult.fail("模具编号不存在！");
        }
        if (null != eamAssetInfoDTO.getStorageStatus() && 1 == eamAssetInfoDTO.getStorageStatus()) {
            return RetResult.fail("模具状态已在库！不需要入库");
        }
        if (eamAssetInfoDTO.getAssetStatus() == null || !(eamAssetInfoDTO.getAssetStatus() == 1 || eamAssetInfoDTO.getAssetStatus() == 3)) {
            return RetResult.fail("模具状态不是生产/正常状态,无法入库");
        }
        return RetResult.ok(mouldAssetInfoService.getSpotCheck("rk", eamAssetInfoDTO));
    }

    /**
     * 添加/更新管理模具台账
     */
    @ApiOperation("保存模具台账")
    @PostMapping("/save")
    @RequestLogHeader(modular = "模具台账管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamAssetInfoDTO eamAssetInfo) {
        eamAssetInfo.setAssetKind(AssetKind.MOULD.getValue());
        // 数据校验
        RetResult ret = equAssetInfoService.isValid(eamAssetInfo);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }
        RetResult<?> result = assetInfoController.save(eamAssetInfo);
        return result;
    }

    /**
     * 批量删除模具台账
     */
    @ApiOperation("批量删除模具台账")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "模具台账管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamAssetInfoDTO eamAssetInfo) {
        RetResult<?> result = assetInfoController.delete(eamAssetInfo);
        return result;
    }

    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody EamAssetInfoQuery requestParam, HttpServletResponse response) throws Exception {
        requestParam.setAssetKind(AssetKind.MOULD.getValue());
        assetInfoController.export(requestParam, response);
    }

//    /**
//     * 导入资产台账
//     */
//    @PostMapping("upload")
//    public RetResult<?> upload(MultipartFile file) throws IOException {
//        // 读取excel
//        List<EamAssetInfoUploadExcel> items = EasyExcel.read(file.getInputStream(),EamAssetInfoUploadExcel.class,null).autoCloseStream(true).sheet(0).headRowNumber(1).autoTrim(true).doReadSync();
//        eamAssetInfoService.insertBatch(items);
//        return RetResult.ok();
//    }
//
//    /**
//     * 下载模板
//     */
//    @PostMapping("exportTemp")
//    @ApiOperation("下载模板")
//    public void exportTemp(HttpServletResponse response) throws Exception {
//        List<EamAssetInfoUploadExcel> list = new ArrayList<>();
//        list.add(new EamAssetInfoUploadExcel());
//        ExcelUtils.exportExcelToTarget(response, null, list, EamAssetInfoUploadExcel.class);
//    }


    /**
     * 删除模具台账
     */
    @ApiOperation("删除模具台账")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "资产台账管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        RetResult<?> result = assetInfoController.deleteItem(id);
        return result;
    }

    /**
     * 获取点检信息
     *
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamModProductRecordDTO", name = "eamModProductRecord", value = "", required = true)
    })
    @ApiOperation(value = "获取点检信息列表", notes = "获取点检信息列表列表", httpMethod = "POST")
    @PostMapping("getCheckOrderOut")
    public RetResult<EamMouldTaskInfoDetailDTO> getCheckOrderOut(@RequestBody EamMouldTaskInfoDetailDTO dto) {
        return RetResult.ok(mouldAssetInfoService.getSpotCheck("ck", dto));
    }

    /**
     * 添加/更新点检表
     */
    @ApiOperation("保存点检信息表")
    @PostMapping("/saveSpotCheckOut")
    @RequestLogHeader(modular = "保存点检信息表", businessType = "保存")
    public RetResult<?> saveSpotCheckOut(@RequestBody @Valid EamMouldTaskInfoDetailDTO dto) {
        mouldAssetInfoService.saveSpotCheck("ck", dto);
        return RetResult.ok();
    }
}
